package com.xyz.mockapi.importexcel.service.impl;

import com.xyz.mockapi.common.entity.BdSubProductTypeEntity;
import com.xyz.mockapi.common.entity.OnlineBomList;
import com.xyz.mockapi.common.entity.OnlineProductEntity;
import com.xyz.mockapi.common.util.ExcelUtil;
import com.xyz.mockapi.importexcel.service.ImportService;
import com.xyz.mockapi.mainlineorder.service.BdSubProductTypeService;
import com.xyz.mockapi.mainlineorder.service.OnlineBomService;
import com.xyz.mockapi.mainlineorder.service.OnlineProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class ImportServiceImpl implements ImportService {

    private final BdSubProductTypeService bdSubProductTypeService;
    private final OnlineBomService onlineBomService;
    private final OnlineProductService onlineProductService;

    public ImportServiceImpl(
            BdSubProductTypeService bdSubProductTypeService,
            OnlineBomService onlineBomService,
            OnlineProductService onlineProductService) {
        this.bdSubProductTypeService = bdSubProductTypeService;
        this.onlineBomService = onlineBomService;
        this.onlineProductService = onlineProductService;
    }

    // 电驱打刻配置导入
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importBdSubProductType(MultipartFile file) throws IOException {
        List<BdSubProductTypeEntity> list = ExcelUtil.parseExcel(file, BdSubProductTypeEntity.class);
        if (list.isEmpty()) return "文件无数据";
        bdSubProductTypeService.saveBatch(list);
        return "导入成功，共 " + list.size() + " 条记录";
    }

    // BOM数据导入
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importOnlineBom(MultipartFile file) throws IOException {
        List<OnlineBomList> list = ExcelUtil.parseExcel(file, OnlineBomList.class);
        if (list.isEmpty()) return "文件无数据";
        onlineBomService.saveBatch(list);
        return "导入成功，共 " + list.size() + " 条记录";
    }

    // 工单主体信息导入
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importOnlineProduct(MultipartFile file) throws IOException {
        List<OnlineProductEntity> list = ExcelUtil.parseExcel(file, OnlineProductEntity.class);
        if (list.isEmpty()) return "文件无数据";
        onlineProductService.saveBatch(list);
        return "导入成功，共 " + list.size() + " 条记录";
    }
}